package com.example.demo.service.impl;

import com.example.demo.entity.OrderEntity;
import com.example.demo.entity.ProductSkuEntity;
import com.example.demo.mapper.OrderMapper;
import com.example.demo.mapper.ProductSkuMapper;
import com.example.demo.service.OrderService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {

    private final OrderMapper orderMapper;
    
    @Autowired
    public OrderServiceImpl(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }

    @Override
    public List<OrderEntity> findAll() {
        return orderMapper.selectAll();
    }

    @Override
    public Optional<OrderEntity> findById(Long id) {
        return Optional.ofNullable(orderMapper.selectOneById(id));
    }

    @Override
    public OrderEntity update(Long id, OrderEntity orderDetails) {
        OrderEntity order = orderMapper.selectOneById(id);
        if (order == null) {
            throw new RuntimeException("Order not found with id: " + id);
        }

        order.setOrderNumber(orderDetails.getOrderNumber());
        order.setUserId(orderDetails.getUserId());
        order.setTotalAmount(orderDetails.getTotalAmount());
        order.setStatus(orderDetails.getStatus());

        orderMapper.update(order);
        return order;
    }

    @Override
    public void deleteById(Long id) {
        orderMapper.deleteById(id);
    }

    @Override
    public Optional<OrderEntity> findByOrderNumber(String orderNumber) {
        // 需要自定义查询方法
        return null;
    }

    @Override
    public List<OrderEntity> findByUserId(Long userId) {
        // 需要自定义查询方法
        return null;
    }

    @Override
    public List<OrderEntity> findByStatus(String status) {
        // 需要自定义查询方法
        return null;
    }
}